package com.xltt.watchlauncher3.view.menu;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.xltt.watchlauncher3.R;
import com.xltt.watchlauncher3.entity.AppInfoEntity;

import java.util.ArrayList;
import java.util.List;

public class LauncherAdapter extends RecyclerView.Adapter<LauncherAdapter.ViewHolder> {

    private int mIndex = -1;
    public interface OnItemClickListener {
        void onItemClick(View v, int pos);
    }
    public OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    private List<AppInfoEntity> mDatas;
    public LauncherAdapter(List<AppInfoEntity> datas){
        this.mDatas = datas;
    }

    public List<AppInfoEntity> getDatas(){
        return mDatas == null ? new ArrayList<>() : mDatas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.launcher_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //holder.imageView.setImageResource(mDatas.get(position).getImgResouces());
        AppInfoEntity info = mDatas.get(position);
        int ckResouce = info.getCheckedBg();
        //int uckResouce = mDatas.get(position).getUncheckedBg();
        holder.imageView.setImageResource(ckResouce/*position == mIndex ? ckResouce : uckResouce*/);
        holder.imageView.setTag(position);
        if(AppInfoEnumerator.SUPPORT_US_LANGUAGE){
            holder.name.setText(info.getAppLabel());
            holder.name.setTextColor(position == mIndex ? Color.WHITE : Color.GRAY);
        }else{
            holder.name.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView name;

        ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
            name = itemView.findViewById(R.id.name);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(v, getAdapterPosition());
                    }
                }
            });
        }
    }
    public void setPosition(int index){
        this.mIndex = index;
        //notifyDataSetChanged();
    }

}
